package com.codedog.controller;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import com.codedog.constant.BeanNameConstant;
import com.codedog.constant.StrLenConstant;
import com.codedog.entity.properties.CompletionRate;
import com.codedog.enums.ResponseCodeEnum;
import com.codedog.service.CodeDogService;
import com.codedog.utils.CodeDogCookieUtil;
import com.codedog.utils.CodeDogExceptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@EnableAutoConfiguration
@RestController(BeanNameConstant.CODEDOG_CONTROLLER)
public class CodeDogController {
    @Autowired
    private CodeDogService codeDogService;
    @Autowired
    private CompletionRate completionRate;

    private static final List<String> timeList = Arrays.asList("0900", "1100", "1400", "1600", "1730", "1930");

    /**
     * 根据班级获取作业情况
     *
     * @return
     */
    public Integer getLessonRecordsByClass() {
        try {
            codeDogService.getLessonRecordsByClass();
            return ResponseCodeEnum.OK.getCode();
        } catch (Exception e) {
            return CodeDogExceptionUtil.getExceptionCode(ExceptionUtil.getMessage(e));
        }
    }

    /**
     * 根据日期获取作业情况
     *
     * @param dateStr
     * @return
     */
    public Integer getLessonRecordsByDate(String dateStr) {
        try {
            if (StrLenConstant.DATE_STR_LENGTH != StrUtil.length(dateStr)) {
                System.out.println("请输入正确日期格式：（例如：20241012）");
                return ResponseCodeEnum.DATE_FORMAT_ERROR.getCode();
            }
            for (String timeStr : timeList) {
                codeDogService.getLessonRecordsByDateOrTimeSlot(dateStr + timeStr);
            }
            return ResponseCodeEnum.OK.getCode();
        } catch (Exception e) {
            return CodeDogExceptionUtil.getExceptionCode(ExceptionUtil.getMessage(e));
        }
    }

    /**
     * 根据时间获取作业情况
     *
     * @param timeSlotStr
     * @return
     */
    public Integer getLessonRecordsByTimeSlot(String timeSlotStr) {
        try {
            if (StrLenConstant.DATEANDTIME_STR_LENGTH != StrUtil.length(timeSlotStr)) {
                System.out.println("请输入正确格式：具体日期和时间（例如：202410120900）");
                return ResponseCodeEnum.DATE_FORMAT_ERROR.getCode();
            }
            codeDogService.getLessonRecordsByDateOrTimeSlot(timeSlotStr);
            return ResponseCodeEnum.OK.getCode();
        } catch (Exception e) {
            return CodeDogExceptionUtil.getExceptionCode(ExceptionUtil.getMessage(e));
        }

    }

    /**
     * 绩效
     *
     * @return
     */
    public Integer getPerformance() {
        try {
            codeDogService.getPerformance();
            return ResponseCodeEnum.OK.getCode();
        } catch (Exception e) {
            return CodeDogExceptionUtil.getExceptionCode(ExceptionUtil.getMessage(e));
        }
    }

    /**
     * 阶段测
     *
     * @return
     */
    public Integer getLessonPaperReports() {
        try {
            codeDogService.getLessonPaperReports();
            return ResponseCodeEnum.OK.getCode();
        } catch (Exception e) {
            return CodeDogExceptionUtil.getExceptionCode(ExceptionUtil.getMessage(e));
        }
    }

    /**
     * 更新Cookie
     *
     * @param cookie
     * @return
     */
    public Integer updateCookie(String cookie) {
        Integer updateCookieResponseCode = CodeDogCookieUtil.updateCookie(cookie);
        if (ResponseCodeEnum.OK.getCode() == updateCookieResponseCode) {
            completionRate.setCookie(cookie);
        }
        return updateCookieResponseCode;
    }

}
